package com.shuzhi.stationarysource.modules.quartz.controller;

import com.shuzhi.stationarysource.exception.BadRequestException;
import com.shuzhi.stationarysource.modules.quartz.domain.QuartzJob;
import com.shuzhi.stationarysource.modules.quartz.service.QuartzJobService;
import com.shuzhi.stationarysource.modules.quartz.service.dto.JobQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * @ClassName QuartzJobController
 * @Description QuartzJobController
 * @Author 孙峰
 * @Date 2020/3/10 9:36
 * @Version 1.0
 */

@RestController
@Api(tags = "系统:定时任务管理")
@RequestMapping("/api/jobs")
public class QuartzJobController {
    private static final String ENTITY_NAME = "quartzJob";

    private final QuartzJobService quartzJobService;

    public QuartzJobController(QuartzJobService quartzJobService) {
        this.quartzJobService = quartzJobService;
    }

    // @Log("根据id查询定时任务")
    @ApiOperation("根据id查询定时任务")
    @GetMapping("/{id}")
    public ResponseEntity<Object> getJobs(@PathVariable("id") Long id) {
        return new ResponseEntity<>(quartzJobService.findById(id), HttpStatus.OK);
    }

    // @Log("查询定时任务")
    @ApiOperation("查询定时任务")
    @GetMapping
    public ResponseEntity<Object> getJobs(JobQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(quartzJobService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    // @Log("导出任务数据")
    @ApiOperation("导出任务数据")
    @GetMapping(value = "/download")

    public void download(HttpServletResponse response, JobQueryCriteria criteria) throws IOException {
        quartzJobService.download(quartzJobService.queryAll(criteria), response);
    }

    // @Log("导出日志数据")
    @ApiOperation("导出日志数据")
    @GetMapping(value = "/logs/download")

    public void downloadLog(HttpServletResponse response, JobQueryCriteria criteria) throws IOException {
        quartzJobService.downloadLog(quartzJobService.queryAllLog(criteria), response);
    }

    @ApiOperation("查询任务执行日志")
    @GetMapping(value = "/logs")

    public ResponseEntity<Object> getJobLogs(JobQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(quartzJobService.queryAllLog(criteria, pageable), HttpStatus.OK);
    }

    // @Log("新增定时任务")
    @ApiOperation("新增定时任务")
    @PostMapping

    public ResponseEntity<Object> create(@Validated @RequestBody QuartzJob resources) {
        if (resources.getId() != null) {
            throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        return new ResponseEntity<>(quartzJobService.create(resources), HttpStatus.CREATED);
    }

    // @Log("修改定时任务")
    @ApiOperation("修改定时任务")
    @PutMapping

    public ResponseEntity<Object> update(@Validated(QuartzJob.Update.class) @RequestBody QuartzJob resources) {
        quartzJobService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    // @Log("更改定时任务状态")
    @ApiOperation("更改定时任务状态")
    @PutMapping(value = "/{id}")
    public ResponseEntity<Object> updateIsPause(@PathVariable Long id) {
        quartzJobService.updateIsPause(quartzJobService.findById(id));
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    //  @Log("执行定时任务")
    @ApiOperation("执行定时任务")
    @PutMapping(value = "/exec/{id}")
    public ResponseEntity<Object> execution(@PathVariable Long id) {
        quartzJobService.execution(quartzJobService.findById(id));
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    // @Log("删除定时任务")
    @ApiOperation("删除定时任务一个id")
    @DeleteMapping(value ="/{id}")
    public ResponseEntity<Object> deleteById(@PathVariable Long id) {
        quartzJobService.deleteById(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    // @Log("删除定时任务")
    @ApiOperation("删除定时任务")
    @DeleteMapping(value ="/ids/{ids}")
    public ResponseEntity<Object> delete(@PathVariable("ids") Long[] ids) {
        quartzJobService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
